Events = {}


def addListener(event, listener):
    global Events
    if not hasListener(event):
        Events[event] = []

    Events[event].append(listener)


def removeListener(event, listener):
    global Events
    if not hasListener(event):
        return

    if listener not in Events[event]:
        return

    Events[event].remove(listener)


def hasListener(event):
    return event in Events.keys()


def clearListener(event):
    global Events
    if not hasListener(event):
        return

    Events[event].clear()


def dispatch(event, *args):
    global Events
    if not hasListener(event):
        return

    listeners = Events[event]
    for listener in listeners:
        listener(*args)
